
package com.ctsi.question.service;

import com.ctsi.commons.util.UtilValidate;
import com.ctsi.commons.util.id.UUIDGenerator;
import com.ctsi.commons.util.response.Page;
import com.ctsi.framework.mybatis.support.MybatisGenericDao;
import com.ctsi.question.entity.Answers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


/**
 * Answers Service 类
 * @author   liuhx
 * @description  Answers
 * @created	 Wed Nov 28 11:51:00 CST 2018
 */

@Service
public class AnswersService {
	private static final Logger logger=LoggerFactory.getLogger(AnswersService.class);
	@Autowired
	private MybatisGenericDao dao;
	
	/**
	 * 添加
	 */
	public int insertAnswers(Answers answers){
		if(UtilValidate.isEmpty(answers.getId()))
			answers.setId(UUIDGenerator.getId());
		logger.debug("insert {}",answers);
		return  dao.insert(answers);
	}	
	/**
	 * 采纳
	 */	
	public int updateAnswersById(Answers answers){
		logger.debug("updateAnswersById {}",answers);
		return dao.update(Answers.class.getName()+".updateAnswersById", answers);
	}

	
	/*
	* 根据id查询详情
	* */
	public Answers selectAnwserById(String id){
		if(UtilValidate.isEmpty(id))
			return null;
		logger.debug("selectAnwserById {}",id);
		return (Answers)dao.queryForObject(Answers.class.getName()+".selectAnwserById", id);
	}

	/**
	 * 删除Answers对象(is_default=1)
	 */
	public int deleteAnswersById(Answers answers){
		logger.debug("deleteAnswersById {}",answers);
		return dao.update(Answers.class.getName()+".deleteAnswersById", answers);
	}

	/** 
	 * 多条件查询返回Answers列表
	 */
	public Page<Answers> selectMulitCondition(Map<String,Object> param , int start , int pageSize){
		logger.debug("selectMulitCondition {}",param);
		if(pageSize<0||pageSize>2000)
			pageSize=Page.DEFAULT_PAGE_SIZE;
		if(start!=-1)
			return dao.queryPage(Answers.class.getName()+".selectMulitCondition", param, start, pageSize);
		else{
			throw new RuntimeException("参数不正确 !!");
		}
	}
	/*
	查询解答数量根据question_id
	* selectAnswerCount
	* */
	public  Object  selectAnswerCount(String  questionId){
		logger.debug("selectAnswerCount {}",questionId);
		return 	 dao.queryForObject(Answers.class.getName() + ".selectAnswerCount", questionId);
	}

}
